import { useMutation, useQuery } from "@tanstack/react-query";
import { message } from "antd";
import { client } from "../request";

export const useAnalysisChainWithID = () => {
  return useMutation({
    mutationFn: (params: any) => {
      return client.post({
        url: "/task/load_by_id/",
        data: params,
      });
    },
  });
};

export const useUploadFile = () => {
  return useMutation({
    mutationFn: (params: any) => {
      return client.post({
        url: "/upload/",
        headers: { "Content-Type": "multipart/form-data" },
        data: params,
      });
    },
  });
};

export const useAnalysisChainWithFile = () => {
  return useMutation({
    mutationFn: (params: any) => {
      return client.post({
        url: "/task/load/",
        headers: { "Content-Type": "multipart/form-data" },
        data: params,
      });
    },
  });
};

export const useGetTaskList = (page: number) => {
  return useQuery({
    queryFn: () => {
      return client
        .get({
          url: `/task/?page=${page}`,
        })
        .then(
          (res) => {
            return Promise.resolve(res);
          },
          (err) => {
            message.error("获取任务列表失败");
            console.log(err);
          }
        );
    },
    queryKey: ["tasks", page],
    retry: false,
  });
};

export const useGetStatics = () => {
  return useQuery({
    queryFn: () => {
      return client
        .get({
          url: "/task/statistics/",
        })
        .then(
          (res) => {
            return Promise.resolve(res);
          },
          (err) => {
            message.error("获取统计数据失败");
            console.log(err);
          }
        );
    },
    queryKey: ["tasks-statistics"],
    retry: false,
  });
};

export const useTaskRun = () => {
  return useMutation({
    mutationFn: (params: any) => {
      return client.post({
        url: "/task/",
        data: params,
      });
    },
  });
};

export const useCreateWorkspace = () => {
  return useMutation({
    mutationFn: (params: any) => {
      return client.post({
        url: "/workspace/",
        data: params,
      });
    },
  });
};

export const useListFilter = () => {
  return useQuery({
    queryFn: () => {
      return client.get({
        url: "/filter/",
      });
    },
    staleTime: 0,
    queryKey: ["filter"],
  });
};
